package com.supinfo.bonjoursupinfo.adapters;

import android.content.Context;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.supinfo.bonjoursupinfo.R;
import com.supinfo.bonjoursupinfo.activities.SinglePhotosViewerActivity;
import com.supinfo.bonjoursupinfo.database.PictureBDD;
import com.supinfo.bonjoursupinfo.utils.Util;

public class PhotosListAdapter extends CursorAdapter{

	private int layoutID;
	@SuppressWarnings("unused")
	private static final String TAG_IMAGE_LOADING = "PhotosList Image Loading";
	private static final String BASE_URL = "http://37.187.46.134:80/";
	
	public static int getDefaultLayoutID(){
		return R.layout.item_photolist;
	}
	
	public PhotosListAdapter(Context context, Cursor c, int flags) {
		super(context, c, flags);

		layoutID = PhotosListAdapter.getDefaultLayoutID();
		
		ImageLoaderConfiguration.Builder imgl_config_builder = new ImageLoaderConfiguration.Builder(context);
		
		DisplayImageOptions.Builder displayoptions = new DisplayImageOptions.Builder();
		displayoptions.showImageOnFail(R.drawable.holo_light_alerts_and_states_error);
		displayoptions.showStubImage(R.drawable.holo_light_content_picture);
		displayoptions.cacheOnDisc();
		displayoptions.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
		
		imgl_config_builder.defaultDisplayImageOptions(displayoptions.build());
	
		ImageLoaderConfiguration imgl_config = imgl_config_builder.build();
		ImageLoader.getInstance().init(imgl_config);
	}
	
	public int getLayoutID(){
		return layoutID;
	}

	@Override
	public void bindView(View view, final Context context, Cursor c) {
		ImageView imgV = (ImageView) view.findViewById(R.id.image);
		TextView txtV_titre = (TextView) view.findViewById(R.id.title);
		TextView txtV_description = (TextView) view.findViewById(R.id.description);
		
		final String title = c.getString(c.getColumnIndex(PictureBDD.COL_NAME));
		final String desc = c.getString(c.getColumnIndex(PictureBDD.COL_DESC));
		txtV_titre.setText(title);
		txtV_description.setText(desc);
		
		final String url = BASE_URL+c.getString(c.getColumnIndex(PictureBDD.COL_URL));;
		ImageLoader.getInstance().displayImage(url, imgV);
		
		if(Util.getScreenOrientation(context) == Configuration.ORIENTATION_PORTRAIT){
			imgV.getLayoutParams().height = Util.getScreenWidth(context)
					- 2*Math.round((Util.convertDpToPixel(10, context)));
		} else if (Util.getScreenOrientation(context) == Configuration.ORIENTATION_LANDSCAPE){
			imgV.getLayoutParams().width = Util.getScreenHeight(context)
					- 2*Math.round((Util.convertDpToPixel(10, context)));;
		}
		
		if(c.getPosition() == 0){
			((LinearLayout.LayoutParams)
					((LinearLayout) view.findViewById(R.id.wrap)).getLayoutParams())
					.topMargin = Math.round(Util.convertDpToPixel(10, context));
		}
		
		imgV.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Bundle acPar = new Bundle();
				acPar.putString(SinglePhotosViewerActivity.EXTRA_URL, url);
				acPar.putString(SinglePhotosViewerActivity.EXTRA_TITLE, title);
				acPar.putString(SinglePhotosViewerActivity.EXTRA_DESC, desc);
				Util.startActivity(context, SinglePhotosViewerActivity.class, acPar);
			}
		});
		
	}

	@Override
	public View newView(Context context, Cursor c, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View nView = inflater.inflate(layoutID, parent, false);

		bindView(nView, context, c);
		return nView;
	}
	 
}
